package com.jm.offer.easy;

import com.jm.offer.ListNode;

/**
 * @Description https://leetcode-cn.com/problems/fan-zhuan-lian-biao-lcof/
 * @date 2022/4/14 7:53
 */
public class _24反转链表 {
    public static void main(String[] args) {
        ListNode head = new ListNode(1,new ListNode(2, new ListNode(3,new ListNode(4))));
        ListNode node = reverseList(head);
        while (node != null){
            System.out.println(node.val);
            node = node.next;
        }
    }

    public static ListNode reverseList(ListNode head) {
        ListNode prev = null;
        ListNode curr = head;
        while (curr != null) {
            // 获取‘新的头节点’
            ListNode next = curr.next;
            // 翻转next指向
            curr.next = prev;
            // 交换前后节点
            prev = curr;
            curr = next;
        }
        return prev;
    }
}
